package com.hotel.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hotel.dao.AuthorityMapper;
import com.hotel.dao.RoleOfAuthorityMapper;
import com.hotel.entity.Authority;
import com.hotel.entity.Role;
import com.hotel.entity.RoleOfAuthority;
import com.hotel.entity.User;
import com.hotel.entity.UserRole;
import com.hotel.service.AuthorityService;
@Service("authorityService")
@Transactional(transactionManager="txManager")
public class AuthorityServiceImpl implements AuthorityService {
	
	@Resource
	private AuthorityMapper authorityMapper;
	
	@Resource
	private RoleOfAuthorityMapper RoleOfAuthorityMapper;
	
	@Override
	public int deleteByPrimaryKey(Integer id) {
		return authorityMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Authority record) {
		return authorityMapper.insert(record);
	}

	@Override
	public Authority selectByPrimaryKey(Integer id) {
		return authorityMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKey(Authority record) {
		return authorityMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<Authority> findAllAuthoritys(Integer pageNo,Integer pageSize) {
		return authorityMapper.findAllAuthoritys((pageNo-1)*pageSize,pageSize);
	}

	@Override
	public List<RoleOfAuthority> findAllRoleOfAuthoritysByUser() {
		List<RoleOfAuthority> RoleOfAuthoritys = RoleOfAuthorityMapper.findAllRoleOfAuthoritysByUser();
		List<RoleOfAuthority> newRoleOfAuthoritys = new ArrayList<RoleOfAuthority>();
		Authority newAuthority = null;
		//遍历全部角色
		for (int i = 0; i < RoleOfAuthoritys.size(); i++) {
			//拿出角色i
			Role role = RoleOfAuthoritys.get(i).getRole();
			//拿出权限i
			newAuthority = RoleOfAuthoritys.get(i).getAuthority();
			//拿出角色权限关系表id
			Integer roleAuthorityId = RoleOfAuthoritys.get(i).getRoleAuthorityId();
			
			//从1开始，第一个角色不与自己比较,最后一个角色不与自己比较
			for (int j = i+1; j < RoleOfAuthoritys.size(); j++) {
				if(roleAuthorityId==null)continue;
				//newRole = userRoles.get(i).getRole();	
				if(RoleOfAuthoritys.get(j).getRole()==null){
					continue;
				}
				String roleName=RoleOfAuthoritys.get(j).getRole().getRoleName();
				//将角色i与其它角色比较  如果有角色名相同
				if(role.getRoleName().equals(roleName)){
					//角色i的另一个权限
					Authority nextAuthority = RoleOfAuthoritys.get(j).getAuthority();
					//同一角色的权限放到一起
					newAuthority.setPermissionName(newAuthority.getPermissionName()+","+nextAuthority.getPermissionName());
					//清除掉已经合并的角色的另一个权限
					RoleOfAuthoritys.remove(j);
					j--;
				}
			}
			//将角色权限重新组装
			RoleOfAuthority RoleOfAuthority = new RoleOfAuthority();
			RoleOfAuthority.setRole(role);
			RoleOfAuthority.setAuthority(newAuthority);
			RoleOfAuthority.setRoleAuthorityId(roleAuthorityId);
			//添加到新集合
			newRoleOfAuthoritys.add(RoleOfAuthority);
		}
		return newRoleOfAuthoritys;
	}
	
  

}
